/*     */ package com.zimbra.cs.account.callback;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.account.AttributeCallback;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DomainCOSMaxAccounts
/*     */   extends AttributeCallback
/*     */ {
/*     */   public void preModify(CallbackContext context, String attrName, Object attrValue, Map attrsToModify, Entry entry)
/*     */     throws ServiceException
/*     */   {
/*  37 */     String attr = "zimbraDomainCOSMaxAccounts";
/*  38 */     String addAttr = "+" + attr;
/*  39 */     String delAttr = "-" + attr;
/*     */     
/*  41 */     Map<String, String> cur = new HashMap();
/*     */     
/*     */ 
/*  44 */     if (entry != null) {
/*  45 */       Set<String> curValues = entry.getMultiAttrSet(attr);
/*  46 */       for (String v : curValues)
/*     */       {
/*  48 */         Pair<String, String> parsed = parse(v, false);
/*  49 */         if (parsed != null) {
/*  50 */           cur.put(parsed.getFirst(), parsed.getSecond());
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*  55 */     for (Object e : attrsToModify.entrySet()) {
/*  56 */       Map.Entry<String, Object> keyVal = (Map.Entry)e;
/*  57 */       String aName = (String)keyVal.getKey();
/*  58 */       List<String> vals = getMultiValue(keyVal.getValue());
/*     */       
/*  60 */       if (delAttr.equals(aName))
/*     */       {
/*  62 */         for (String v : vals) {
/*  63 */           if (v.length() != 0)
/*     */           {
/*     */ 
/*  66 */             Pair<String, String> parsed = parse(v, true);
/*  67 */             cur.remove(parsed.getFirst());
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*  73 */     for (Object e : attrsToModify.entrySet()) {
/*  74 */       Map.Entry<String, Object> keyVal = (Map.Entry)e;
/*  75 */       String aName = (String)keyVal.getKey();
/*  76 */       List<String> vals = getMultiValue(keyVal.getValue());
/*     */       
/*  78 */       if (attr.equals(aName))
/*     */       {
/*  80 */         checkDup(new HashMap(), vals);
/*  81 */       } else if (addAttr.equals(aName))
/*     */       {
/*  83 */         checkDup(cur, vals);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void checkDup(Map<String, String> curVals, List<String> newVals) throws ServiceException {
/*  89 */     for (String v : newVals)
/*  90 */       if (v.length() != 0)
/*     */       {
/*     */ 
/*  93 */         Pair<String, String> parsed = parse(v, true);
/*  94 */         String other = (String)curVals.get(parsed.getFirst());
/*  95 */         if (other != null) {
/*  96 */           throw ServiceException.INVALID_REQUEST("cannot contain multiple values for the same cos " + (String)parsed.getFirst() + ": " + (String)parsed.getSecond() + ", " + other, null);
/*     */         }
/*     */         
/*  99 */         curVals.put(parsed.getFirst(), parsed.getSecond());
/*     */       }
/*     */   }
/*     */   
/*     */   private Pair<String, String> parse(String value, boolean throwOnError) throws ServiceException {
/* 104 */     String[] parts = value.split(":");
/* 105 */     if (parts.length != 2) {
/* 106 */       if (throwOnError) {
/* 107 */         throw ServiceException.INVALID_REQUEST("invalid format", null);
/*     */       }
/* 109 */       return null;
/*     */     }
/* 111 */     return new Pair(parts[0], parts[1]);
/*     */   }
/*     */   
/*     */   public void postModify(CallbackContext context, String attrName, Entry entry) {}
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/callback/DomainCOSMaxAccounts.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */